package com.yequan.hbb.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yequan.hbb.R;
import com.yequan.hbb.entries.TypeListEntry;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/10/15.
 */

public class DetailAdapter extends RecyclerView.Adapter<DetailAdapter.DetailViewHolder> {

    private Context context;

    private List<TypeListEntry.DataBean.ClassifyBean.ListBean> mListBeen;

    public DetailAdapter(Context context) {
        this.context = context;
        mListBeen = new ArrayList<>();
    }

    public void addAll(List<TypeListEntry.DataBean.ClassifyBean.ListBean> listBeen) {
        mListBeen.clear();
        // notifyItemRangeRemoved(0,mListBeen.size());
        mListBeen.addAll(listBeen);
        notifyDataSetChanged();
    }


    @Override
    public DetailViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View inflate = LayoutInflater.from(context).inflate(R.layout.wl_type_detail, parent, false);

        return new DetailViewHolder(inflate);
    }

    @Override
    public void onBindViewHolder(DetailViewHolder holder, int position) {

        LinearLayout itemView;
        if (holder.itemView.getTag() != null) {
            //  TextView current = holder.tv_title_sub;
            itemView = (LinearLayout) holder.itemView;
            itemView.removeViewsInLayout(1, itemView.getChildCount() - 1);
        }

        TypeListEntry.DataBean.ClassifyBean.ListBean listBean = mListBeen.get(position);
        holder.tv_title_sub.setText(listBean.getName() + "");
        List<TypeListEntry.DataBean.ClassifyBean.ListBean.ListBeanSub> list = listBean.getList();
        int size = list.size();
        if (size != 0) {
            for (int i = 0; i < size; ) {
                if (i < size) {
                    View inflate = LayoutInflater.from(context).inflate(R.layout.wl_type_item_3, null, false);
                    TextView tv1 = (TextView) inflate.findViewById(R.id.tv_item_1);
                    TextView tv2 = (TextView) inflate.findViewById(R.id.tv_item_2);
                    TextView tv3 = (TextView) inflate.findViewById(R.id.tv_item_3);
                    tv1.setText(list.get(i).getName());
                    i++;
                    if (i < size) {
                        tv2.setText(list.get(i).getName());
                        i++;
                    } else {
                        tv2.setVisibility(View.INVISIBLE);
                        tv3.setVisibility(View.INVISIBLE);
                    }
                    if (i < size) {
                        tv3.setText(list.get(i).getName());
                        i++;
                    } else {
                        tv3.setVisibility(View.INVISIBLE);
                    }
                    ((ViewGroup) holder.itemView).addView(inflate);
                }
            }

            holder.itemView.setTag(true);
        }
    }

    @Override
    public void onViewDetachedFromWindow(DetailViewHolder holder) {
        super.onViewDetachedFromWindow(holder);
        holder = null;
    }

    @Override
    public int getItemCount() {
        return mListBeen.size();
    }

    public static class DetailViewHolder extends RecyclerView.ViewHolder {

        private final TextView tv_title_sub;

        public DetailViewHolder(View itemView) {
            super(itemView);
            tv_title_sub = ((TextView) itemView.findViewById(R.id.tv_title_sub));
        }
    }
}
